package com.manong.bookstore.produce.controller;

import java.util.*;

import com.manong.bookstore.produce.feign.AdminFeignService;
import com.manong.bookstore.produce.feign.StoreFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.manong.bookstore.produce.entity.ProduceEntity;
import com.manong.bookstore.produce.service.ProduceService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;



/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-26  21:35:59
 */
@RestController
@RequestMapping("produce/produce")
public class ProduceController {
    @Autowired
    private ProduceService produceService;

    @Autowired
    private StoreFeignService storeFeignService;

    @Autowired
    private AdminFeignService adminFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = produceService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 店铺查询所有订单
     */
    @RequestMapping("/listStore/{storeId}")
    public R listByStoreId(@RequestParam Map<String, Object> params,@PathVariable("storeId") long storeId){
        PageUtils page = produceService.queryPage(params,storeId);
        List<ProduceEntity> list= (List<ProduceEntity>) page.getList();
        Object[] classifies=null;
        Object[] categories=null;
        try{
            for (int i=0;i<list.size();i++) {
                String name=storeFeignService.getCategoryName(list.get(i).getCategoryId());
                String classifyName=adminFeignService.getClassifyName(list.get(i).getClassifyId());
                list.get(i).setCategoryName(name);
                list.get(i).setClassifyName(classifyName);
            }
            page.setList(list);
            classifies=adminFeignService.listClassifyNames(new HashMap<String, Object>());
            categories=storeFeignService.getCategoryNames(new HashMap<String, Object>(), (int) storeId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok("分类或目录获取失败").put("page", page);
        }
        return R.ok().put("page", page)
                .put("classifies",classifies)
                .put("categories",categories);
    }

    /**
     * 根据分类id查询所有订单
     */
    @RequestMapping("/listClassifyId/{classifyId}")
    public R listByClassifyId(@RequestParam Map<String, Object> params,@PathVariable("classifyId") long classifyId){
        PageUtils page = produceService.queryPageClassify(params,classifyId);
        return R.ok().put("page", page);
    }

    /**
     * 根据目录id查询所有订单
     */
    @RequestMapping("/listCategoryId/{categoryId}")
    public R listByCategoryId(@RequestParam Map<String, Object> params,@PathVariable("categoryId") long categoryId){
        PageUtils page = produceService.queryPageCategory(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{produceId}")
    public R info(@PathVariable("produceId") Integer produceId){
		ProduceEntity produce = produceService.getById(produceId);
        Object[] classifies=null;
        Object[] categories=null;
		try{
            classifies=adminFeignService.listClassifyNames(new HashMap<String, Object>());
            categories=storeFeignService.getCategoryNames(new HashMap<String, Object>(),produce.getStoreId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok("分类或目录获取失败").put("produce", produce);
        }

        return R.ok().put("produce", produce)
                .put("classifies",classifies)
                .put("categories",categories);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ProduceEntity produce){
		produceService.save(produce);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ProduceEntity produce){
		produceService.updateById(produce);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] produceIds){
		produceService.removeByIds(Arrays.asList(produceIds));

        return R.ok();
    }

}
